package com.fet.redeem.action;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.fet.redeem.model.Customer;
import com.fet.redeem.model.Gift;
import com.fet.redeem.service.RedeemService;
import com.opensymphony.xwork2.ActionSupport;

public class RedeemAction extends ActionSupport {

	private RedeemService redeemService;

	public RedeemService getRedeemService() {
		return redeemService;
	}

	public void setRedeemService(RedeemService redeemService) {
		this.redeemService = redeemService;
	}
	
	public String loadRedeem(){
		return SUCCESS;
	}
	
	public String loadCustomerList(){
		try{
			List<Customer> custList = redeemService.getAllCustList();
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("customerList", custList);
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String loadGiftList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String custName = request.getParameter("custName");
		
		try{
			Customer customer = redeemService.getCustomerByName(custName);
			customer.setCp1giftList(new ArrayList<String>());
			customer.setCp2giftList(new ArrayList<String>());
			
			redeemService.checkGift(customer);
			
			List<Gift> giftList = new ArrayList<Gift>();
			List<String> cp1giftList = customer.getCp1giftList();
			List<String> cp2giftList = customer.getCp2giftList();
			for(String gift:cp1giftList){
				Gift gift1 = redeemService.getGiftById(gift);
				giftList.add(gift1);
			}
			for(String gift:cp2giftList){
				Gift gift1 = redeemService.getGiftById(gift);
				giftList.add(gift1);
			}
			
			request.setAttribute("customer", customer);
			request.setAttribute("giftList", giftList);
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
}
